<?php
class Db_News
{
    
    public static function get_news($delcache = false){
        if(ArrCache::is_not_cached('news_arr',0,'',$delcache)){
            if(!$delcache){
                //added for All Cities
                CGlobal::$all_news[0] = array();

                $sql_str = "SELECT * FROM ".TABLE_PREFIX."news WHERE status = 1 ORDER by time_c ASC";
                $result  = DB::query($sql_str);
                while($news = mysql_fetch_assoc($result)){
                    $news["bkname"] = String::safe_title($news['tittle']);
                    CGlobal::$all_news[$news['id']] 			= $news;
                    CGlobal::$news_name[strtolower($news['bkname'])] 		= $news;
                }

                ArrCache::set(array('array_news'=>CGlobal::$all_news,'news_name'=>CGlobal::$news_name));
            }else{
                foreach (CGlobal::$my_server as $server){//Tạo lại file js cho news
                    @fopen("http://$server/newsgen.php","r");
                }
            }
        }
        else{
            $news_arr	 			= ArrCache::get();

            CGlobal::$all_news  	= $news_arr['array_news'];
            CGlobal::$news_name 	= $news_arr['news_name'];
        }
    }

    public static function insertreview($data){
        if(DB::insert('news_comment', $data, false, __FILE__ . ' line ' . __LINE__)) {
            return true;
        }else{
            return false;
        }
    }

    public static function showallreview($news_id, $condition="") {
        if($condition)
        {
            $condition = ' AND '.$condition;
        }
        $reviews = DB::select_all('news_comment', 'news_id='. $news_id . $condition);
        return $reviews;
    }

    public static function getNewsById($id){
        static $policyHotel;
        if (!isset($policyHotel[$id])) {
            $policyHotel[$id] = DB::select('news', 'id=' . $id, __FILE__ . ' line ' . __LINE__);
        }
        return $policyHotel[$id];
    }
}
